package com.example.modlemain.ui.main

import android.graphics.Color
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.text.Html
import android.text.Html.ImageGetter
import android.text.SpannableString
import android.text.Spanned
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.example.modlemain.R
import com.example.mylibrary.util.TestUtil
import com.view.text.addTag


class MainFragment : Fragment() {

    companion object {
        fun newInstance() = MainFragment()
    }

    private val viewModel: MainViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // TODO: Use the ViewModel
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return inflater.inflate(R.layout.modulemain_fragment_main, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val textView: TextView = view.findViewById(R.id.message)
        val ttv: TextView = view.findViewById(R.id.ttv)
        textView.text = "结果=${TestUtil.add()}"
        textView.text = Html.fromHtml(getAddress(), getImageGetter(), null)
        SpanUtils.applyRoundBackground(textView, "码说", Color.RED)

        val layoutInflater = LayoutInflater.from(context)
        val view = layoutInflater.inflate(R.layout.title_tag1, null)
        ttv.addTag(view)
        ttv.addTag(view, marginLeft = 10, marginRight = 10)
    }

    /**
     * Desc:通过img标签加载tab图片，后面拼接地址字符串
     * @return string
     */
    private fun getAddress(): String {
        return ((("<img src='" + R.mipmap.image_xd).toString() + "'/>  "
                + "<img src='" + R.mipmap.image_xd).toString() + "'/>  "
                + "码说的是事实是事实是事实得瑟得瑟得瑟得瑟码码码码码码码大叔广东省东莞市遇到过大会上讲话的是觉得还是觉得还是决定回家反对反对反对反对大幅度")
    }

    /**
     * Desc:getDrawable传参source为img标签传入的图片资源id，最终返回图片的Drawable对象
     * @return ImageGetter
     */
    private fun getImageGetter(): ImageGetter {
        return ImageGetter { source ->
            val drawable: Drawable =
                requireContext().applicationContext.getResources().getDrawable(source.toInt())
            drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
            drawable
        }
    }


}